package com.blyat.xsoft.auth.aotuconfigure;

import com.blyat.xsoft.auth.security.SecurityUserService;
import com.blyat.xsoft.kernel.component.security.SecurityLoginFailureHandler;
import com.blyat.xsoft.kernel.component.security.SecurityLoginSuccessHandler;
import com.blyat.xsoft.kernel.component.security.SecurityLogoutSuccessHandler;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Auther: syh
 * @Date: 2020/7/14
 * @Description:
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true) // 控制权限注解
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginProcessingUrl("/login")
                .passwordParameter("password")
                .usernameParameter("username")
                .successHandler(new SecurityLoginSuccessHandler())
                .failureHandler(new SecurityLoginFailureHandler())
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessHandler(new SecurityLogoutSuccessHandler());
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(securityUserService());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new PasswordEncoder() {

            @Override
            public String encode(CharSequence charSequence) {
                return Hashing.md5().hashString(charSequence.toString(), Charsets.UTF_8).toString();
            }

            @Override
            public boolean matches(CharSequence charSequence, String password) {
                return password
                        .equals(Hashing.md5().hashString(charSequence.toString(), Charsets.UTF_8).toString());
            }
        };
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Bean
    public UserDetailsService securityUserService() {
        return new SecurityUserService();
    }
}
